{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 1.8 \u5b57\u5178\u7684\u8fd0\u7b97\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u600e\u6837\u5728\u6570\u636e\u5b57\u5178\u4e2d\u6267\u884c\u4e00\u4e9b\u8ba1\u7b97\u64cd\u4f5c\uff08\u6bd4\u5982\u6c42\u6700\u5c0f\u503c\u3001\u6700\u5927\u503c\u3001\u6392\u5e8f\u7b49\u7b49\uff09\uff1f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8003\u8651\u4e0b\u9762\u7684\u80a1\u7968\u540d\u548c\u4ef7\u683c\u6620\u5c04\u5b57\u5178\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "prices = {\n    'ACME': 45.23,\n    'AAPL': 612.78,\n    'IBM': 205.55,\n    'HPQ': 37.20,\n    'FB': 10.75\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u5bf9\u5b57\u5178\u503c\u6267\u884c\u8ba1\u7b97\u64cd\u4f5c\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528 zip() \u51fd\u6570\u5148\u5c06\u952e\u548c\u503c\u53cd\u8f6c\u8fc7\u6765\u3002\n\u6bd4\u5982\uff0c\u4e0b\u9762\u662f\u67e5\u627e\u6700\u5c0f\u548c\u6700\u5927\u80a1\u7968\u4ef7\u683c\u548c\u80a1\u7968\u503c\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "min_price = min(zip(prices.values(), prices.keys()))\n# min_price is (10.75, 'FB')\nmax_price = max(zip(prices.values(), prices.keys()))\n# max_price is (612.78, 'AAPL')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7c7b\u4f3c\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528 zip() \u548c sorted() \u51fd\u6570\u6765\u6392\u5217\u5b57\u5178\u6570\u636e\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "prices_sorted = sorted(zip(prices.values(), prices.keys()))\n# prices_sorted is [(10.75, 'FB'), (37.2, 'HPQ'),\n#                   (45.23, 'ACME'), (205.55, 'IBM'),\n#                   (612.78, 'AAPL')]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6267\u884c\u8fd9\u4e9b\u8ba1\u7b97\u7684\u65f6\u5019\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f zip() \u51fd\u6570\u521b\u5efa\u7684\u662f\u4e00\u4e2a\u53ea\u80fd\u8bbf\u95ee\u4e00\u6b21\u7684\u8fed\u4ee3\u5668\u3002\n\u6bd4\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c31\u4f1a\u4ea7\u751f\u9519\u8bef\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "prices_and_names = zip(prices.values(), prices.keys())\nprint(min(prices_and_names)) # OK\nprint(max(prices_and_names)) # ValueError: max() arg is an empty sequence"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u5728\u4e00\u4e2a\u5b57\u5178\u4e0a\u6267\u884c\u666e\u901a\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u4eec\u4ec5\u4ec5\u4f5c\u7528\u4e8e\u952e\uff0c\u800c\u4e0d\u662f\u503c\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "min(prices) # Returns 'AAPL'\nmax(prices) # Returns 'IBM'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u7ed3\u679c\u5e76\u4e0d\u662f\u4f60\u60f3\u8981\u7684\uff0c\u56e0\u4e3a\u4f60\u60f3\u8981\u5728\u5b57\u5178\u7684\u503c\u96c6\u5408\u4e0a\u6267\u884c\u8fd9\u4e9b\u8ba1\u7b97\u3002\n\u6216\u8bb8\u4f60\u4f1a\u5c1d\u8bd5\u7740\u4f7f\u7528\u5b57\u5178\u7684 values() \u65b9\u6cd5\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "min(prices.values()) # Returns 10.75\nmax(prices.values()) # Returns 612.78"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0d\u5e78\u7684\u662f\uff0c\u901a\u5e38\u8fd9\u4e2a\u7ed3\u679c\u540c\u6837\u4e5f\u4e0d\u662f\u4f60\u60f3\u8981\u7684\u3002\n\u4f60\u53ef\u80fd\u8fd8\u60f3\u8981\u77e5\u9053\u5bf9\u5e94\u7684\u952e\u7684\u4fe1\u606f\uff08\u6bd4\u5982\u90a3\u79cd\u80a1\u7968\u4ef7\u683c\u662f\u6700\u4f4e\u7684\uff1f\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u53ef\u4ee5\u5728 min() \u548c max() \u51fd\u6570\u4e2d\u63d0\u4f9b key \u51fd\u6570\u53c2\u6570\u6765\u83b7\u53d6\u6700\u5c0f\u503c\u6216\u6700\u5927\u503c\u5bf9\u5e94\u7684\u952e\u7684\u4fe1\u606f\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "min(prices, key=lambda k: prices[k]) # Returns 'FB'\nmax(prices, key=lambda k: prices[k]) # Returns 'AAPL'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f46\u662f\uff0c\u5982\u679c\u8fd8\u60f3\u8981\u5f97\u5230\u6700\u5c0f\u503c\uff0c\u4f60\u53c8\u5f97\u6267\u884c\u4e00\u6b21\u67e5\u627e\u64cd\u4f5c\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "min_value = prices[min(prices, key=lambda k: prices[k])]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u524d\u9762\u7684 zip() \u51fd\u6570\u65b9\u6848\u901a\u8fc7\u5c06\u5b57\u5178\u201d\u53cd\u8f6c\u201d\u4e3a (\u503c\uff0c\u952e) \u5143\u7ec4\u5e8f\u5217\u6765\u89e3\u51b3\u4e86\u4e0a\u8ff0\u95ee\u9898\u3002\n\u5f53\u6bd4\u8f83\u4e24\u4e2a\u5143\u7ec4\u7684\u65f6\u5019\uff0c\u503c\u4f1a\u5148\u8fdb\u884c\u6bd4\u8f83\uff0c\u7136\u540e\u624d\u662f\u952e\u3002\n\u8fd9\u6837\u7684\u8bdd\u4f60\u5c31\u80fd\u901a\u8fc7\u4e00\u6761\u7b80\u5355\u7684\u8bed\u53e5\u5c31\u80fd\u5f88\u8f7b\u677e\u7684\u5b9e\u73b0\u5728\u5b57\u5178\u4e0a\u7684\u6c42\u6700\u503c\u548c\u6392\u5e8f\u64cd\u4f5c\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9700\u8981\u6ce8\u610f\u7684\u662f\u5728\u8ba1\u7b97\u64cd\u4f5c\u4e2d\u4f7f\u7528\u5230\u4e86 (\u503c\uff0c\u952e) \u5bf9\u3002\u5f53\u591a\u4e2a\u5b9e\u4f53\u62e5\u6709\u76f8\u540c\u7684\u503c\u7684\u65f6\u5019\uff0c\u952e\u4f1a\u51b3\u5b9a\u8fd4\u56de\u7ed3\u679c\u3002\n\u6bd4\u5982\uff0c\u5728\u6267\u884c min() \u548c max() \u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u5982\u679c\u6070\u5de7\u6700\u5c0f\u6216\u6700\u5927\u503c\u6709\u91cd\u590d\u7684\uff0c\u90a3\u4e48\u62e5\u6709\u6700\u5c0f\u6216\u6700\u5927\u952e\u7684\u5b9e\u4f53\u4f1a\u8fd4\u56de\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "prices = { 'AAA' : 45.23, 'ZZZ': 45.23 }\nmin(zip(prices.values(), prices.keys()))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "max(zip(prices.values(), prices.keys()))"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}